********************************************************************************
* analyze_worker.do
* Purpose: Produce main worker-level DiD event-study results (Tables 3-6,
*          Figures 3-6).
*
* Table 3  : DiD estimates for impact of M&As on all incumbent workers.
*            Two rows: Workers at Target, Workers at Acquirer.
*            Col 1: log(Earnings) - all workers
*            Col 2: Transition probability (job change indicator)
*            Col 3: log(Earnings) - stayers only (present_at_firm == 1)
*            Input : $data/worker_matched.dta
*            Output: $out/table3.csv
*
* Figure 3 : Event-study plots corresponding to Table 3.
*            A: log(Earnings) - Acquirer vs. Target workers
*            B: Transition probability - Acquirer vs. Target workers
*            C: log(Earnings) for stayers - Acquirer vs. Target workers
*            Input : $data/worker_matched.dta
*            Output: $out/Figure3_A.pdf, $out/Figure3_B.pdf, $out/Figure3_C.pdf
*
* Table 4  : DiD estimates for workers moving from targets (matched_acq == 0)
*            within the first two years after the event (first two mover cohorts).
*            Col 1: log(Earnings) - all movers from targets
*            Col 2: Transition probability
*            Col 3: log(Earnings) - movers to acquirer firm
*            Col 4: log(Earnings) - movers to other (non-acquirer) firms
*            Col 5: Probability of transition to a different industry
*            Input : $data/worker_matched.dta
*            Output: $out/table4.csv
*
* Figure 4 : Event-study plots corresponding to Table 4.
*            A: log(Earnings) - movers from targets
*            B: Transition probability - movers from targets
*            C: log(Earnings) by destination (to acquirer vs. to other firms)
*            D: Probability of transition to a different industry
*            Input : $data/worker_matched.dta
*            Output: $out/Figure4_A.pdf ... $out/Figure4_D.pdf
*
* Table 5  : DiD estimates for changes in average firm characteristics for
*            workers moving from targets (first two mover cohorts).
*            Col 1: Employer Fixed Effect (AKM firm wage premium)
*            Col 2: Match Effect (worker-employer match component)
*            Col 3: log(Revenue) of destination firm
*            Col 4: Profit Margins of destination firm
*            Input : $data/worker_matched.dta
*            Output: $out/table5.csv
*
* Figure 5 : Event-study plots corresponding to Table 5.
*            A: Employer Fixed Effect, B: Match Effect,
*            C: log(Revenue),         D: Profit Margins
*            Input : $data/worker_matched.dta
*            Output: $out/Figure5_A.pdf ... $out/Figure5_D.pdf
*
* Table 6  : DiD heterogeneity estimates for workers moving from targets,
*            by pre-event worker tenure and within-firm earnings distribution.
*            Panel A (top): By Tenure (4 years vs. 7+ years of tenure)
*              Rows: Post x Treated (4yr baseline), Post x Treated x 7+ Years
*              Cols: log(Earnings), Transition
*            Panel B (bottom): By Within-Firm Earnings Distribution (Q1 vs. Q5)
*              Rows: Post x Treated (Q1 baseline), Post x Treated x Q5
*              Cols: log(Earnings), Transition
*            Input : $data/worker_matched_w_tenure.dta (Panel A),
*                    $data/worker_matched_w_wage.dta   (Panel B)
*            Output: $out/table6.csv
*
* Figure 6 : Event-study plots corresponding to Table 6.
*            Panel A - By Tenure:
*              A: log(Earnings) by tenure (4yr vs. 7+yr)
*              B: Transition by tenure (4yr vs. 7+yr)
*            Panel B - By Within-Firm Earnings Distribution:
*              C: log(Earnings) by earnings quintile (Q1 vs. Q5)
*              D: Transition by earnings quintile (Q1 vs. Q5)
*            Input : $data/worker_matched_w_tenure.dta,
*                    $data/worker_matched_w_wage.dta
*            Output: $out/Figure6_A.pdf ... $out/Figure6_D.pdf
*
* Regression specification:
*   FE: id + event-time x 2-digit NAICS (i.t##i.naics)
*   Standard errors two-way clustered at the worker and firm level.
*   Outcomes winsorized at the 5th/95th percentile by year.
*   Average post-event impact = average of ds_7 through ds_12 (years 0 to 5).
********************************************************************************
*        Worker Analysis      *
********************************************************************************
set more off

********************************************************************************
							* Main Analysis Sample *
********************************************************************************
use $data/worker_matched, clear

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)
gen y_moved			= moved
gen y_moved_inds	= moved_ind
gen y_emp			= log(PD7_AvgEmp_NonZero)
gen y_rev			= log(total_revenue)
gen y_netprof		= (total_revenue - total_expense)/total_revenue
gen y_total_wage	= total_wage

label var y_t4earn 			"log(Earnings)"
label var y_moved 			"Transition"
label var y_moved_inds		"To Different Industry"
label var y_rev 			"log(Revenue)"
label var y_emp 			"log(Employment)"
label var y_netprof 		"Profit Margins"
label var y_total_wage		"Earnings"


global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"
global ind_var 			"treated##ds_*"
global average_impact 	"(1.treated#1.ds_7 + 1.treated#1.ds_8 + 1.treated#1.ds_9 + 1.treated#1.ds_10 + 1.treated#1.ds_11 + 1.treated#1.ds_12)/6"
global average_impact2 	"(1.treated#1.ds_8 + 1.treated#1.ds_9 + 1.treated#1.ds_10 + 1.treated#1.ds_11 + 1.treated#1.ds_12)/5"

**# Table 3 - Column 1 and 2 - top row
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_acq == 0

estimates clear
local varlist "t4earn moved"
foreach v in `varlist'{
	eststo es0_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m0_`v': lincomest $average_impact
	
	estimates restore es0_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es0_`v'
}

**# Table 3 - Column 1 and 2 - bottom row
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_acq == 1

local varlist "t4earn moved"
foreach v in `varlist'{
	eststo es1_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)
	
	eststo m1_`v': lincomest $average_impact
	
	estimates restore es1_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'
  	estimates store es1_`v'
}



**# Table 3 - Column 3 - top row
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_acq == 0
keep if present_at_firm == 1

local varlist "t4earn"
foreach v in `varlist'{
	eststo es0_`v'_stay: reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m0_`v'_stay: lincomest $average_impact
	
	estimates restore es0_`v'_stay
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'
	estimates store es0_`v'_stay
}

**# Table 3 - Column 3 - bottom row
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_acq == 1
keep if present_at_firm == 1

local varlist "t4earn"
foreach v in `varlist'{
	eststo es1_`v'_stay: reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)
	
	eststo m1_`v'_stay: lincomest $average_impact
	
	estimates restore es1_`v'_stay
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  	
	estimates store es1_`v'_stay
}

#delimit ;
local first = 1;

forvalue h = 0/1{;
	if `first'{;
		local replace_append "replace";
		local title "f collabels(none) noobs title("") mtitle("log(Earnings)" "Transition" "log(Earnings) - Stayers") coeflabel((1) "Workers at Target")";
		local first = 0;
	};
	else  {;
		local replace_append "append";
		local title "f collabels(none) noobs title("") nomtitles coeflabel((1) "Workers at Acquirer")";
	};

	esttab m`h'_t4earn m`h'_moved m`h'_t4earn_stay using "$out/table3.csv", 
	`replace_append' `title'
	starlevel(* 0.10 ** 0.05 *** 0.01) 
	cells( b(star fmt(3)) se(par fmt(3)) ) type;
	
	esttab es`h'_t4earn es`h'_moved es`h'_t4earn_stay using "$out/table3.csv", 
	append plain f collabels(none) keep("") nomtitles 
	stats(ym r2_a N, fmt(%9.2f %9.3f %9.0fc) label("Mean at t=-1" "Adj. R squared" "Firm-Year Obs.")) type;
};

#delimit cr

**# Figure 3 - A
#delimit ;
make_eventstudy t4earn,
	models("es1_t4earn es0_t4earn")
	colors("dknavy dkorange")
	labels("WorkeratAcquirer WorkeratTarget")
	fmt("%9.2fc") ylab("-0.06(0.02)0.06")
	fig("Figure3") subfig("A");

#delimit cr

**# Figure 3 - B
#delimit ;
make_eventstudy moved,
	models("es1_moved es0_moved")
	colors("dknavy dkorange")
	labels("WorkeratAcquirer WorkeratTarget")
	fmt("%9.2fc") ylab("-0.1(0.1)0.3")
	fig("Figure3") subfig("B");

#delimit cr

**# Figure 3 - C
#delimit ;
make_eventstudy t4earn_stay,
	models("es1_t4earn_stay es0_t4earn_stay")
	colors("dknavy dkorange")
	labels("WorkeratAcquirer WorkeratTarget")
	fmt("%9.2fc") ylab("-0.06(0.02)0.06")
	fig("Figure3") subfig("C");

#delimit cr

**# Table 4 - Columns 1 and 2
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_acq == 0

* first two cohort of movers
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2

estimates clear
local varlist "t4earn moved"
foreach v in `varlist'{
	eststo es_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)
	
	eststo m_`v': lincomest $average_impact
	
	estimates restore es_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'
	estimates store es_`v'
}

**# Figure4 - A
#delimit ;
make_eventstudy t4earn,
	models("es_t4earn")
	colors("dkorange")
	labels("MoversfromTargets")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("Figure4") subfig("A");

#delimit cr

**# Figure 4 - B
#delimit ;
make_eventstudy moved,
	models("es_moved")
	colors("dkorange")
	labels("MoversfromTargets")
	fmt("%9.2fc") ylab("-0.1(0.2)0.7")
	fig("Figure4") subfig("B");

#delimit cr

**# Table 4 - Columns 3 and 4
restore, preserve
rename other_party_id tmp_id
gegen other_party_id = mode(tmp_id), by(id)

gsort id year

sysdir set PLUS "Z:\VRDC-PROJ-6730\Moon_6730\ado"
run "Z:\VRDC-PROJ-6730\Moon_6730\ado\_\_gtools_internal.mata"

** transition by destination
* (1) move within the parent company (2) move to acquirer firm (3) move to different firm
gen		move_type	=	0	if ever_moved_post	==	0
replace	move_type	=	1	if (UltimateParentID_syn	==	UltimateParentID_syn[_n-1] ) & year == first_move_year
replace	move_type	=	2	if (other_party_id == entid_syn | other_party_id == UltimateParentID_syn) & year == first_move_year & mi(move_type)
replace move_type	=	3	if year == first_move_year	& mi(move_type)
replace	move_type	=	. 	if treated == 0

gegen matched_move_type		= firstnm(move_type), 			by(pairid year_prior)

gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_acq == 0

* first two cohort of movers
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	

local varlist "t4earn"
foreach v in `varlist'{
	eststo es_acquirer: reghdfe y_`v' $ind_var if matched_move_type == 2, a($FE) vce( cluster $clusters ) tol(1e-6)
	
	eststo m_acquirer: lincomest $average_impact
	
	estimates restore es_acquirer
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'
	estimates store es_acquirer
}

local varlist "t4earn"
foreach v in `varlist'{
	eststo es_others: reghdfe y_`v' $ind_var if matched_move_type == 3, a($FE) vce( cluster $clusters ) tol(1e-6)
	
	eststo m_others: lincomest $average_impact
	
	estimates restore es_others
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'
	estimates store es_others
}

**# Table 4 - Column 5
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_acq == 0

* first two cohort of movers
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	

local varlist "moved_ind"
foreach v in `varlist'{
	eststo es_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)
	
	eststo m_`v': lincomest $average_impact
	
	estimates restore es_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'
	estimates store es_`v'
}

#delimit ;
local first = 1;

if `first'{;
	local replace_append "replace";
	local title "f collabels(none) noobs title("") mtitle("log(Earnings)" "Transition" "log(Earnings)" "log(Earnings)" "Transition") coeflabel((1) "Workers Moving from Targets")";
	local first = 0;
};

esttab m_t4earn m_moved m_acquirer m_others m_moved_ind using $out/table4.csv,
`replace_append' `title'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

	
esttab es_t4earn es_moved es_acquirer es_others es_moved_ind using $out/table4.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2f %9.3f %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

#delimit cr

**# Figure4 - C
#delimit ;
make_eventstudy t4earn,
	models("es_acquirer es_others")
	colors("dkorange maroon")
	labels("ToAcquirer ToOtherFirms")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("Figure4") subfig("C");

#delimit cr

**# Figure 4 - D
#delimit ;
make_eventstudy moved_ind,
	models("es_moved_ind")
	colors("dkorange")
	labels("MoversfromTargets")
	fmt("%9.2fc") ylab("-0.1(0.2)0.7")
	fig("Figure4") subfig("D");

#delimit cr

********************************************************************************
							* Table 5 *
********************************************************************************
restore, preserve
gen y_fe = fe
gen y_met = logearnings_met

label var y_met				"Match Effect"
label var y_fe				"Employer Fixed Effect"

* winsorize the data
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_acq == 0

* first two cohort of movers
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2

local varlist "fe met rev netprof" 
estimates clear
foreach v in `varlist'{
	eststo es_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)
	
	eststo m_`v': lincomest $average_impact2
	
	estimates restore es_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'
	estimates store es_`v'
}

#delimit ;
local first = 1;

if `first'{;
	local replace_append "replace";
	local title "f collabels(none) noobs title("") mtitle("Employer Fixed Effect" "Match Effect" "log(Revenue)" "Profit Margins") coeflabel((1) "Workers Moving from Targets")";
	local first = 0;
};

esttab m_* using $out/table5.csv,
`replace_append' `title'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

	
esttab es_* using $out/table5.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2f %9.3f %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

#delimit cr

**# Figure 5 - A
#delimit ;
make_eventstudy fe,
	models("es_fe")
	colors("dkorange")
	labels("MoversfromTargets")
	fmt("%9.2fc") ylab("-0.02(0.02)0.08")
	fig("Figure5") subfig("A");

#delimit cr

**# Figure 5 - B
#delimit ;
make_eventstudy met,
	models("es_met")
	colors("dkorange")
	labels("MoversfromTargets")
	fmt("%9.2fc") ylab("-0.10(0.04)0.02")
	fig("Figure5") subfig("B");

#delimit cr

**# Figure 5 - C
#delimit ;
make_eventstudy rev,
	models("es_rev")
	colors("dkorange")
	labels("MoversfromTargets")
	fmt("%9.2fc") ylab("-0.20(0.2)0.80")
	fig("Figure5") subfig("C");

#delimit cr

**# Figure 5 - D
#delimit ;
make_eventstudy netprof,
	models("es_netprof")
	colors("dkorange")
	labels("MoversfromTargets")
	fmt("%9.2fc") ylab("-0.02(0.02)0.08")
	fig("Figure5") subfig("D");

#delimit cr
restore, not
********************************************************************************
							* With Tenure Sample *
********************************************************************************
use $data/worker_matched_w_tenure, clear

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)
gen y_moved			= moved

label var y_t4earn 			"log(Earnings)"
label var y_moved 			"Transition"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"

**# Table 6 - top panel
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_acq == 0

* first two cohort of movers
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	

gen 	matched_tenure_years = 0 if matched_tenure == 1		//3 and 4 years
replace matched_tenure_years = 1 if matched_tenure >= 3		//above 7 years

estimates clear
local varlist "t4earn moved"
foreach v in `varlist'{
	
	eststo es_`v': reghdfe y_`v' matched_tenure_years##treated##ds_*,  a($FE) vce( cluster $clusters ) tol(1e-6)
	
	eststo treated_`v': lincomest (1bn.treated#1bn.ds_7+1bn.treated#1bn.ds_8+1bn.treated#1bn.ds_9+1bn.treated#1bn.ds_10+1bn.treated#1bn.ds_11+1bn.treated#1bn.ds_12)/6
	
	estimate restore es_`v'
	
	eststo ddd_treated_`v': lincomest (1bn.matched_tenure_years#1bn.treated#1bn.ds_7 + 1bn.matched_tenure_years#1bn.treated#1bn.ds_8 + 1bn.matched_tenure_years#1bn.treated#1bn.ds_9 + 1bn.matched_tenure_years#1bn.treated#1bn.ds_10 + 1bn.matched_tenure_years#1bn.treated#1bn.ds_11 + 1bn.matched_tenure_years#1bn.treated#1bn.ds_12)/6
		
	estimates restore es_`v'
	summ `e(depvar)' if treated == 1 & t == -1 & matched_tenure_years == 0
	estadd scalar ym0 = `r(mean)'
	summ `e(depvar)' if matched_tenure_years == 0
	estadd scalar n0 = `r(N)'
	summ `e(depvar)' if treated == 1 & t == -1 & matched_tenure_years == 1
	estadd scalar ym1 = `r(mean)'
	summ `e(depvar)' if matched_tenure_years == 1
	estadd scalar n1 = `r(N)'
	estimates store es_`v'
}

#delimit ;
local first = 1;

if `first'{;
	local replace_append "replace";
	local title "f collabels(none) noobs title("Panel A: By Tenure") mtitle("log(Earnings)" "Transition") coeflabel((1) "Post X Treated")";
	local first = 0;
};

esttab treated_t4earn treated_moved using $out/table6.csv,
`replace_append' `title'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab ddd_treated_t4earn ddd_treated_moved using $out/table6.csv,
append f collabels(none) noobs title("") nomtitles coeflabel((1) "Post X Treated X 7+ Years of Tenure")
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es_t4earn es_moved using $out/table6.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym0 ym1 r2_a n0 n1, fmt(%9.2f %9.2f %9.3f %9.0fc %9.0fc) label(
"Mean at t=-1 (4 Years of Tenure)"
"Mean at t=-1 (7+ Years of Tenure)"
"Adj. R squared" 
"Worker-Year Obs. (4 Years of Tenure)"
"Worker-Year Obs. (7+ Years of Tenure)")) type;

#delimit cr

local varlist "t4earn moved"
foreach v in `varlist'{
	
	eststo es0_`v': reghdfe y_`v' treated##ds_* if matched_tenure_years == 0,  a($FE) vce( cluster $clusters ) tol(1e-6)
	estimate restore es0_`v'
	
	eststo es1_`v': reghdfe y_`v' treated##ds_* if matched_tenure_years == 1,  a($FE) vce( cluster $clusters ) tol(1e-6)
	estimate restore es1_`v'
}

**# Figure 6 - A
#delimit ;
make_eventstudy t4earn,
	models("es0_t4earn es1_t4earn")
	colors("dkorange maroon")
	labels("4YearsofTenure 7+YearsofTenure")
	fmt("%9.2fc") ylab("-0.1(0.05)0.05")
	fig("Figure6") subfig("A");

#delimit cr

**# Figure 6 - B
#delimit ;
make_eventstudy moved,
	models("es0_moved es1_moved")
	colors("dkorange maroon")
	labels("4YearsofTenure 7+YearsofTenure")
	fmt("%9.2fc") ylab("0.0(0.2)0.8")
	fig("Figure6") subfig("B");

#delimit cr

restore, not
********************************************************************************
						* With Within-Firm Wage Sample *
********************************************************************************
use $data/worker_matched_w_wage, clear

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)
gen y_moved			= moved

label var y_t4earn 			"log(Earnings)"
label var y_moved 			"Transition"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"

**# Table 6 - bottom panel
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_acq == 0

* first two cohort of movers
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	

estimates clear
local varlist "t4earn moved"
foreach v in `varlist'{
	
	eststo es_`v': reghdfe y_`v' matched_wage_5##treated##ds_*,  a($FE) vce( cluster $clusters ) tol(1e-6)
	
	eststo treated_`v': lincomest (1bn.treated#1bn.ds_7+1bn.treated#1bn.ds_8+1bn.treated#1bn.ds_9+1bn.treated#1bn.ds_10+1bn.treated#1bn.ds_11+1bn.treated#1bn.ds_12)/6
	
	estimate restore es_`v'
	
	eststo ddd_treated_`v': lincomest (5bn.matched_wage_5#1bn.treated#1bn.ds_7 + 5bn.matched_wage_5#1bn.treated#1bn.ds_8 + 5bn.matched_wage_5#1bn.treated#1bn.ds_9 + 5bn.matched_wage_5#1bn.treated#1bn.ds_10 + 5bn.matched_wage_5#1bn.treated#1bn.ds_11 + 5bn.matched_wage_5#1bn.treated#1bn.ds_12)/6
		
	estimates restore es_`v'
	summ `e(depvar)' if treated == 1 & t == -1 & matched_wage_5 == 1
	estadd scalar ym0 = `r(mean)'
	summ `e(depvar)' if matched_wage_5 == 1
	estadd scalar n0 = `r(N)'
	summ `e(depvar)' if treated == 1 & t == -1 & matched_wage_5 == 5
	estadd scalar ym1 = `r(mean)'
	summ `e(depvar)' if matched_wage_5 == 5
	estadd scalar n1 = `r(N)'
	estimates store es_`v'
	
}

#delimit ;
local first = 1;

if `first'{;
	local replace_append "append";
	local title "f collabels(none) noobs title("Panel B: By Earnings Quintile") nomtitles coeflabel((1) "Post X Treated")";
	local first = 0;
};

esttab treated_t4earn treated_moved using $out/table6.csv,
`replace_append' `title'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab ddd_treated_t4earn ddd_treated_moved using $out/table6.csv,
append f collabels(none) noobs title("") nomtitles coeflabel((1) "Post X Treated X Q5")
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es_t4earn es_moved using $out/table6.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym0 ym1 r2_a n0 n1, fmt(%9.2f %9.2f %9.3f %9.0fc %9.0fc) label(
"Mean at t=-1 (Q1 = 1)"
"Mean at t=-1 (Q5 = 1)"
"Adj. R squared" 
"Worker-Year Obs. (Q1 = 1)"
"Worker-Year Obs. (Q5 = 1)")) type;

#delimit cr

local varlist "t4earn moved"
foreach v in `varlist'{
	
	eststo es0_`v': reghdfe y_`v' treated##ds_* if matched_wage_5 == 1,  a($FE) vce( cluster $clusters ) tol(1e-6)
	estimate restore es0_`v'
	
	eststo es1_`v': reghdfe y_`v' treated##ds_* if matched_wage_5 == 5,  a($FE) vce( cluster $clusters ) tol(1e-6)
	estimate restore es1_`v'
}

**# Figure 6 - C
#delimit ;
make_eventstudy t4earn,
	models("es0_t4earn es1_t4earn")
	colors("dkorange maroon")
	labels("Q1 Q5")
	fmt("%9.2fc") ylab("-0.1(0.05)0.05")
	fig("Figure6") subfig("C");

#delimit cr

**# Figure 6 - D
#delimit ;
make_eventstudy moved,
	models("es0_moved es1_moved")
	colors("dkorange maroon")
	labels("Q1 7+Q5")
	fmt("%9.2fc") ylab("0.0(0.2)0.8")
	fig("Figure6") subfig("D");

#delimit cr